package com.note.es.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;

import java.util.List;

public class JSONUtils {

    protected final static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object source, String name, Object value) {
            if (value == null)
                return null;
            return value;
        }
    };

    /**
     * 将对象转换为JSON
     */
    public static String c(Object o) {
        return JSON.toJSONString(o, filter);
    }

    /**
     * 将对象转换为JSON
     */
    public static String c(Object o, SerializerFeature... features) {
        return JSON.toJSONString(o, filter, features);
    }

    /**
     * 将JSON转换为对象
     */
    public static <T> T p(String json, Class<T> clazz) {
        if (json == null || clazz == null) {
            return null;
        }
        return JSON.parseObject(json, clazz);
    }

    /**
     * 将JSON转换为数组对象
     */
    public static <E> List<E> pa(String json, Class<E> clazz) {
        if (json == null || clazz == null) {
            return null;
        }
        return JSON.parseArray(json, clazz);
    }

}
